﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Citi.RoutineBase;
using Citi.PTSConsolidation.Domain.Utils;
using Citi.PTSConsolidation.Domain;
using System.Configuration;

namespace Citi.PTSConsolidation.SchedulerUI
{
    public class FinancialTypeExtraction : IExecutableRoutine
    {
        private static string PtsConnectionString { get { return ConfigurationManager.AppSettings["PtsConnectionString"]; } }
        private static string EntrepotConnectionString { get { return ConfigurationManager.AppSettings["EntrepotConnectionString"]; } }

        public IEnumerable<RoutineParameter> Parameters { get { return null; } }

        public void Rollback(ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {

        }

        public void Start(DateTime scheduledDate, ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            DropPreviousData(currentAction);

            currentAction.Description = "Copy has started";

            var query = FinancialType.EntrepotQueryString;

            currentAction.Description = query;

            SqlHelper.CopyDataFromAnotherDatabase(EntrepotConnectionString, query, "FinancialTypeTemp",
                    PtsConnectionString, FinancialType.ColumnMappings);

            currentAction.Description = "Copy has finished";

            MergeData(currentAction);
        }

        private static void DropPreviousData(ICurrentAction currentAction)
        {
            currentAction.Description = "Dropping previous data";
            var query = FinancialType.QueryDuplicateData;
            currentAction.Description = query;
            SqlHelper.ExecuteNonQuery(query, PtsConnectionString);
        }

        private static void MergeData(ICurrentAction currentAction)
        {
            currentAction.Description = "Merging data";
            var query = FinancialType.MergeData;
            currentAction.Description = query;
            SqlHelper.ExecuteNonQuery(query, PtsConnectionString);
        }

        public void Stop(ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {

        }
    }
}
